﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

using Microsoft.AspNetCore.SignalR;

namespace IM
{
    public class Startup
    {
        public const string CorsName = "SignalRCors";
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            services.AddMvc();
            services.AddSignalR();
            services.AddCors(op => {
                op.AddPolicy(CorsName, set => {
                    set.SetIsOriginAllowed(origin => true).AllowAnyHeader()
                    .AllowAnyMethod().AllowCredentials();
                });
            });
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseCors(CorsName);
            app.UseSignalR(routes =>
            {
                routes.MapHub<ChatsHub>("/ChatsHub");
            });
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                name: "default",
                template: "{controller}/{action}/{id?}",
                defaults: new { controller = "Home", action = "Index" });
            });
        }
    }
}
